%{

#include "assembler.h"
#include <stdio.h>
#define YYSTYPE double

void yyerror(char const *);
int main(int argc, char **argv);

%}

%token NEWLINE
%token CONSTANT, LABEL, REGISTER
%token ADD, SUB
%token MOV, CMP
%token AND, OR, NOT
%token CLR, NEG
%token SHL, SHR
%token BRZ, BRN, BRE, BRL, BRG, BRC, JMP

%%

Program:Instructions { printf( "Sintaxe correta.\n Programa compilado.\n" ); }
       ;

Instructions:                    
            |Instructions Instruction
            ;

Instruction:NEWLINE
           |UnaryInstruction  NEWLINE
           |BinaryInstruction NEWLINE
           ;

UnaryInstruction:UnaryOperation REGISTER { printf( "Unary operation on register.\n" ); }
                |UnaryOperation Pointer  { printf( "Unary operation on pointer.\n"  ); }
                |UnaryOperation
                |LabelComand
                |LabelComand    LABEL    { printf( "Label command.\n"               ); }
                ;

BinaryInstruction:BinaryOperation CONSTANT ',' REGISTER { printf("#constant, register"); }
                 |BinaryOperation CONSTANT ',' Pointer  { printf("#constant, pointer");  }
                 |BinaryOperation REGISTER ',' REGISTER { printf("#register, register"); }
                 |BinaryOperation REGISTER ',' Pointer  { printf("#register, pointer");  }
                 |BinaryOperation Pointer  ',' REGISTER { printf("#pointer, register");  }
                 |BinaryOperation Pointer  ',' Pointer  { printf("#po+inter, pointer");  }
                 ;


UnaryOperation:NOT
              |CLR
              |NEG
              |SHL
              |SHR
              ;

LabelComand:BRZ
           |BRN
           |BRE
           |BRL
           |BRG
           |BRC
           |JMP
           ;

BinaryOperation:ADD
               |SUB
               |MOV
               |CMP
               |AND
               |OR
               ;

Pointer: '(' REGISTER ')'
       ;


%%

#include "scanner.c"

void yyerror(char const *token) {
    fprintf(stderr, "%s\n", token);
}

int main(int argc, char **argv) {
    printf("Welcome to the X-Machine Assembler version 0.1!\n");
    printf("This is free software by Asad, Magalhães, Naschiptz and Orfanti.\n");

    yyparse();

    printf("Bye!\n");
}
